package com.tsx.response.controller;

import com.tsx.response.entity.dto.UserRequest;
import com.tsx.response.entity.enums.ReturnCodeEnum;
import com.tsx.response.entity.result.R;
import com.tsx.response.entity.vo.UserResponse;
import com.tsx.response.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/user")
@RestController
@Slf4j
public class UserController {
    @PostMapping("/testR")
    public R<UserResponse> testR(@RequestBody UserRequest userRequest) {
        UserResponse userResponse = new UserResponse();
        BeanUtils.copyProperties(userRequest, userResponse);
        userResponse.setId(RandomUtils.nextLong());
        return R.success(userResponse);
    }

    @PostMapping("/testWithoutR")
//    @NoWrapper
    public UserResponse testWithoutR(@RequestBody UserRequest userRequest) {
        UserResponse userResponse = new UserResponse();
        BeanUtils.copyProperties(userRequest, userResponse);
        userResponse.setId(RandomUtils.nextLong());
        return userResponse;
    }

    @GetMapping("/testVoid")
    public void testVoid() {
        System.out.println("true = " + true);
    }

    @GetMapping("/testException")
    public void testException(Integer param) {
        log.info("UserController.testException param:{}", param);
        if (param == 0) {
            throw new BizException(ReturnCodeEnum.RC_PARAM_NOT_ZERO);
        }
    }
}
